<?php

class ApiController extends Controller
{
	public function actionIndex()
	{
		
		$ct = Province::model()->findAll();
		
		
		$xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n";
		$xml .= "<provinces>\n";
		foreach ($ct as $v) {
			
			$data = Book::model()->findAll('province='.$v->id);
			if(!empty($data)){
				$xml.='<province id="'.$v->id.'" name="'.$v->name.'">';
				$xml.='<books>';
				foreach ($data as $vs) {
					$xml .= $this->create_item($vs->id,$vs->title, 'http://----');
				}
				
				$xml.='</books>';
				$xml.='<province>';
			}
			
		}
		$xml .= "</provinces>\n";
		
		echo $xml;
		
	}
	
	
	//  创建XML单项
	function create_item($bookid,$title_data, $url)
	{
		$item = '<book id="'.$bookid.'">';
		$item .= "<title>" . $title_data . "</title>\n";
		$item .= "<url>" . $url . "</url>\n";
		$item .= "</book>\n";

		return $item;
	}

	// Uncomment the following methods and override them if needed
	/*
	public function filters()
	{
		// return the filter configuration for this controller, e.g.:
		return array(
			'inlineFilterName',
			array(
				'class'=>'path.to.FilterClass',
				'propertyName'=>'propertyValue',
			),
		);
	}

	public function actions()
	{
		// return external action classes, e.g.:
		return array(
			'action1'=>'path.to.ActionClass',
			'action2'=>array(
				'class'=>'path.to.AnotherActionClass',
				'propertyName'=>'propertyValue',
			),
		);
	}
	*/
}